import config from '../config/index'
import Vue from 'vue'

const http={
	 headers:{
		'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
	
	},
	
	post:function(url,params,headers){
		url = url.indexOf('http://')> -1 ?url:config.baseUrl + url
		return new Promise(resolve => {
				uni.request({
					url: url, 
					data:{...this.getOptions(),...params},
					method:'POST',
					header:this.reSetHeaders(headers),
					success: (res) => {
						resolve(res.data)
					},
					fail: (err) => {
						resolve(err)
					}
				});
		})
	},
	get:function(url,params,headers){
		return new Promise(resolve => {
				uni.request({
					url: config.baseUrl + url, 
					data:params,
					method:'GET',
					header:this.reSetHeaders(headers),
					success: (res) => {
						resolve(res.data)
					},
					fail: (err) => {
						resolve(err.data)
					}
				});
			
		})
	},
	reSetHeaders(headers){
		return {...this.headers,...headers}
	},
	getOptions(){
		return{
				userId:Vue.prototype.$Store.userInfo.userId || '',
				token:Vue.prototype.$Store.userInfo.token ||'',
				school:Vue.prototype.$Store.userInfo.school ||'',
				roleId:Vue.prototype.$Store.userInfo.roleId ||'',
				bjId:Vue.prototype.$Store.userInfo.bjId ||''
		}
	}
}
export default http